class Solution
{
public:
    int mostFrequentEven(vector<int> &nums)
    {
        unordered_map<int, int> evenCount;
        for (int &num : nums)
        {
            if (!(num & 1))
            {
                if (evenCount.count(num))
                {
                    ++evenCount[num];
                }
                else
                {
                    evenCount[num] = 1;
                }
            }
        }
        if (evenCount.empty())
        {
            return -1;
        }
        int evenNum = INT_MAX;
        int maxEvenCount = 0;
        for (auto &numCountPair : evenCount)
        {
            if (numCountPair.second > maxEvenCount)
            {
                maxEvenCount = numCountPair.second;
                evenNum = numCountPair.first;
            }
            else if (numCountPair.second == maxEvenCount)
            {
                evenNum = min(evenNum, numCountPair.first);
            }
        }
        return evenNum;
    }
};